<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

//使用类
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
//模型
use App\Admin;

//后台管理员控制器
class AdminController extends Controller
{
    //管理员首页
	public function index(){

	    //总数据
        $tot= Admin::count();
        //$admins = DB::table('admin')->paginate(5);
        $admins = Admin::paginate(5);

        //var_dump($admins);
        //加载页面
        return view("admin.admin.index")->with('data',$admins)->with('tot',$tot);
	}

	//添加页面 admin/admin/create GET
    //public function create(){
    //    return view('admin.admin.add');
    //}

    //插入操作 admin/admin post
    public function store(Request $request){
        //name=1&pass=2&repass=3&status=0

        //直接把字符串数组化
        parse_str( $request->str,$arr);
        //var_dump($arr);
        //表单验证规则
        $rules= [
            'name' => 'required|unique:admin|between:6,12',
            'pass' => 'required|same:repass|between:6,12',
        ];

        //表单验证提示信息
        $message=[
            "name.required"=>"请输入用户名",
            "pass.required"=>"请输入密码",
            "name.unique"=>"用户名已存在",
            "pass.same"=>"两次密码不一致",
            "name.between"=>"用户名长度不在6-12位之间",
            "pass.between"=>"密码长度不在6-12位之间",
        ];

        //使用laravel的表单验证
        $validator= Validator::make($arr, $rules ,$message);

        //开始验证
        if($validator->passes()){
            //验证通过添加数据库
            unset($arr['repass']);
            $arr['pass']=Crypt::encrypt($arr['pass']);//密码加密

            $arr['time']=time();//添加时间
            //插入数据库
            if(DB::table("admin")->insert($arr)){
                return 1;
            }else{
                return 0;
            }

        }else{
            //var_dump($validator);
            //具体查看laravel的核心类
            return $validator->getMessageBag()->getMessages();
        }


       /* //移除repass
        unset($arr['repass']);// 销毁单个数组元素

        $arr['time']=time();

        //插入数据库
        if(DB::table('admin')->insert($arr)){
            return 1;
        }else{
            return 0;
        }  */
    }

    //修改页面 admin/admin/{admin}/edit get
    public function edit($id){
        //查询数据库
        $data = Admin::find($id);
       // var_dump($data);
        $data->pass=Crypt::decrypt($data->pass);

        //分配数据  (返回一个页面)
        return view('admin.admin.edit')->with("data",$data);
    }

    //更新操作admin/admin/{admin} put
    public function update(Request $request){
        //name=1&pass=2&repass=3&status=0

        //直接把字符串数组化
        parse_str($request->str, $arr);
        //var_dump($arr);
        //表单验证规则
        $rules = [
           // 'name' => 'required|unique:admin|between:6,12',
            'pass' => 'required|same:repass|between:6,12',
        ];

        //表单验证提示信息
        $message = [
           // "name.required" => "请输入用户名",
            "pass.required" => "请输入密码",
           // "name.unique" => "用户名已存在",
            "pass.same" => "两次密码不一致",
          //  "name.between" => "用户名长度不在6-12位之间",
            "pass.between" => "密码长度不在6-12位之间",
        ];

        //使用laravel的表单验证
        $validator = Validator::make($arr, $rules, $message);

        //开始验证
        if ($validator->passes()) {
            //验证通过添加数据库
            unset($arr['repass']);
            $arr['pass'] = Crypt::encrypt($arr['pass']);//密码加密


            //插入数据库
            if (Admin::where("id",$arr['id'])->update($arr)) {
                return 1;
            } else {
                return 0;
            }

        } else {
            //var_dump($validator);
            //具体查看laravel的核心类
            return $validator->getMessageBag()->getMessages();
        }
    }

    //删除操作 admin/admin/{admin} delete
    public function destroy($id){
        //return $id;
        //删除数据
        if(Admin::find($id)->delete()){
            return "1";
        }else{
            return "0";
        }

    }

    //修改状态的方法
    public function ajaxStatu(Request $request){
        //剔除数据
        $arr = $request->except('_token');
        //print_r($arr);exit;
        if (Admin::where("id",$arr['id'])->update($arr)){
            return 1;
        }else{
            return 0;
        }


    }

	
}
